package com.java.IO.print_stream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class PrintStreamAPI {
    public static void main(String[] args) throws IOException {
        String path = "File和IO流\\com\\java\\files\\byte_stream.txt";
        
        // 创建对象：若父级路径不存在则抛出异常FileNotFoundException，若文件不存在则会创建，若文件已存在则清空文件内数据。布尔类型的参数表示是否开启自动刷新，但字节打印流无缓冲区所以都会自动刷新
        PrintStream printStream1 = new PrintStream(path);
        PrintStream printStream2 = new PrintStream(new File(path));
        PrintStream printStream3 = new PrintStream(path, "UTF-8");
        PrintStream printStream4 = new PrintStream(new FileOutputStream(path));
        PrintStream printStream5 = new PrintStream(new FileOutputStream(path), true);
        PrintStream printStream6 = new PrintStream(new FileOutputStream(path), true, "UTF-8");
        
        // write：写出数据到本地文件，若参数为整数则会转为ASCII对应的字符
        printStream1.write(97); // a
        printStream1.write('9'); // 9
        printStream1.write('中'); // -
        printStream1.write("\r\n".getBytes()); // 换行(Windows\r\n、Linux\n、Mac\r)
        printStream1.write(new byte[]{97, 98, 99, 100}); // abcd
        printStream1.write(new byte[]{97, 98, 99, 100, 101, 102, 103}, 3, 2); // de
        
        // print：写出任意数据到本地文件，不换行，数据原样写出
        printStream1.print(97); // 97
        printStream1.print("9"); // 9
        printStream1.print("中"); // 中
        
        // printf：写出带占位符的数据到本地文件，不换行，数据原样写出
        printStream1.printf("%S%s", "中", "文"); // 中文
        
        // println：写出任意数据到本地文件，换行，数据原样写出
        printStream1.println(97); // 97\r\n
        printStream1.println("9"); // 9\r\n
        printStream1.println("中"); // 中\r\n
        
        // close：关闭流并释放资源
        printStream1.close();
    }
}
